require 'localvirtualnode'
require 'key'
require 'test/unit'

class TestVirtualNodes < Test::Unit::TestCase  
  def test_virtual_node_find_successor
    node = LocalVirtualNode.new(Key.new("valor"), "127.0.0.1", 9000, 1)
    node.create()
    assert_equal(node.find_successor(Key.new("aksjdhka")).node_id, node.node_id, "Error: Ids diferentes.")
    node.leave()
  end
  
  def test_virtual_node_chord_ring
    boot_node = LocalVirtualNode.new(Key.new("valor"), "127.0.0.1", 9001, 1)
    boot_node.create()
    nodes = []
    9.times do |i|
      nodes << LocalVirtualNode.new(Key.new("valor" + i.to_s), "127.0.0.1", 9002 + i, 1)
    end
    
    nodes.each{|node| node.join(boot_node)}
    
    nodes << boot_node
    
    sleep(40)
    i = 0
    nodes.each do |node|
      puts "----------------------------------------------------------------"
      puts "Node: " + i.to_s
      puts "Node ID: " + node.node_id.to_s
      puts "Node Pred:" + node.get_pred_list.node_id.to_s
      puts "Node Succ:" + node.get_succ_list().first.node_id.to_s
      
      assert_equal(node.get_succ_list().first.get_pred_list().node_id, node.node_id, "Error: Node nao e' o pred de seu succ.")
      i += 1
    end
    
   nodes.each{|node| node.leave()} 
  end
  
end
